﻿using System;
using System.Data;
using System.Collections.Generic;

using Clipper.Model;
using System.Collections.ObjectModel;
namespace Clipper.BLL
{
	/// <summary>
	/// TestTubeInfo
	/// </summary>
	public partial class TestTubeInfo
	{
		private readonly Clipper.DAL.TestTubeInfo dal=new Clipper.DAL.TestTubeInfo();
        SampleInfo mSampleInfoBll = new SampleInfo();
		public TestTubeInfo()
		{}
		#region  BasicMethod
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long testTubeID)
		{
			return dal.Exists(testTubeID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.TestTubeInfo model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.TestTubeInfo model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long testTubeID)
		{
			
			return dal.Delete(testTubeID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string testTubeIDlist )
		{
			return dal.DeleteList(testTubeIDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.TestTubeInfo GetModel(long testTubeID)
		{
			
			return dal.GetModel(testTubeID);
		}

		

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public ObservableCollection<Clipper.Model.TestTubeInfo> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public ObservableCollection<Clipper.Model.TestTubeInfo> DataTableToList(DataTable dt)
		{
			ObservableCollection<Clipper.Model.TestTubeInfo> modelList = new ObservableCollection<Clipper.Model.TestTubeInfo>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Clipper.Model.TestTubeInfo model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 通过试管架ID，获得试管列表，包含检测项,样本信息，病人信息
        /// </summary>
        public ObservableCollection<Clipper.Model.TestTubeInfo> GetModelListByShelf(Model.TestTubeSelfInfo shelf)
        {
            DataSet ds = dal.GetList("TestTubeSelfID="+shelf.testTubeSelfID);
            ObservableCollection<Clipper.Model.TestTubeInfo> modelList = new ObservableCollection<Clipper.Model.TestTubeInfo>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.TestTubeInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(ds.Tables[0].Rows[n]);
                    if (model != null)
                    {
                        model.ShelfBarcode = shelf.barcode;
                        model.SampleInfo = mSampleInfoBll.GetSampleInfoAndItems(model.SampleID);
                        
                        if (model.SampleInfo.DetectItems != null)//填充检测项到试管中
                        {
                            model.DetectItems = model.SampleInfo.DetectItems;
                        }
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

		#endregion  ExtensionMethod
	}
}

